package com.uhomecp.lambda.lambda7;

import com.google.common.collect.Lists;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @description: ExampleEmployee
 * @create: 2020-01-15 11:18
 **/
public class ExampleEmployee {
    private static List<Employee> employeeList = Lists.newArrayList();

    static{
        employeeList.add(Employee.builder().name("Matt").salary(5000).office("New York").build());
        employeeList.add(Employee.builder().name("Steve").salary(6000).office("London").build());
        employeeList.add(Employee.builder().name("Carrie").salary(20000).office("New York").build());
        employeeList.add(Employee.builder().name("Peter").salary(7000).office("New York").build());
        employeeList.add(Employee.builder().name("Pat").salary(8000).office("London").build());
        employeeList.add(Employee.builder().name("Tammy").salary(29000).office("Shanghai").build());
    }

    public static void main(String[] args) {
        //anyMatch
        boolean b = employeeList.stream().anyMatch(employee -> ("London").equals(employee.getOffice()));
        System.out.println(b);

        //返回所有salary大于6000
        boolean b1 = employeeList.stream().allMatch(employee -> employee.getSalary() > 6000);
        System.out.println(b1);

        //找出工资最高
        Optional<Employee> max = employeeList.stream().max((e1, e2) -> Integer.compare(e1.getSalary(), e2.getSalary()));
        System.out.println(max);

        //返回姓名列表
        List<String> nameList = employeeList.stream().map(employee -> employee.getName()).collect(Collectors.toList());
        System.out.println(nameList);

        //List转换成Map
        Map<String, Integer> listToMap = employeeList.stream().collect(Collectors.toMap(key -> key.getName(), value -> value.getSalary()));
        System.out.println(listToMap);

        //统计办公室是New York的个数
        List<Employee> collect = employeeList.stream().filter(employee -> ("New York").equals(employee.getOffice())).collect(Collectors.toList());
        System.out.println(collect);

        //List转换为Set
        Set<Employee> listToSet = employeeList.stream().map(employee -> employee).distinct().collect(Collectors.toSet());
//        System.out.println(listToSet);

        //查找办公室地点是New York的员工，findAny:统计第一个
        Optional<Employee> any = employeeList.stream().filter(employee -> ("New York").equals(employee.getOffice())).findAny();
        System.out.println(any);

        //按照工资的降序来列出员工信息
        List<Employee> collect1 = employeeList.stream().sorted((e1, e2) -> Integer.compare(e1.getSalary(), e2.getSalary())).collect(Collectors.toList());
        System.out.println("那锅"+collect1);

        //按照名字的升序列出员工信息
        List<Employee> collect2 = employeeList.stream().sorted((e1, e2) -> e1.getName().compareTo(e2.getName())).collect(Collectors.toList());
        System.out.println(collect2);

        //获取工资最高的前2条员工信息
        List<Employee> collect3 = employeeList.stream().sorted((e1, e2) -> Integer.compare(e2.getSalary(), e1.getSalary())).limit(2).collect(Collectors.toList());
        System.out.println(collect3);

        //获取平均工资
        OptionalDouble average = employeeList.stream().mapToInt(employee -> employee.getSalary()).average();
        System.out.println("平均工资:" + average);

        //New York办公室平均工资:
        OptionalDouble average1 = employeeList.stream().filter(employee -> "New York".equals(employee.getName())).mapToInt(employee -> employee.getSalary()).average();
        System.out.println("New York办公室平均工资:" + average1);
    }
}
